package com.github.maojx0630.translate.core;

import com.github.maojx0630.translate.TranslateUtil;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

/**
 * @author 毛家兴
 **/
@Intercepts({@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class TranslateInterceptor implements Interceptor {

    private final TranslateUtil translateUtil;

    public TranslateInterceptor(TranslateUtil translateUtil) {
        this.translateUtil = translateUtil;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 执行其他拦截器后的结果处理
        Object result = invocation.proceed();
        return translateUtil.translate(result);
    }
}
